<?php
$loader->requireOnce('includes/CorePayment/BillImporter.abstract.php');
$loader->requireOnce('includes/CorePayment/ParacliniqueFeeItem.class.php');
$loader->requireOnce('ordo/TicketItem.class.php');
class OutParacliniqueImporter extends BillImporter {
	private $radItemList = array();
	private $labItemList = array();

    public function readData() {
    	$field      =   L10nHelper::getLocale();
        $l10Table   =   L10nHelper::L10nTable();
        $l10Key     =   L10nHelper::L10nKey();

        $ticketId   =   $this->storage->getTicketId();

        // Get all radiology items of the ticket
        $sql    =   "SELECT DISTINCT s.service_id, l.$field service_name, ri.normal_price, sum(rsi.normal_price) additional_price,
                        p.price as insurance_price, p.enum_group_type
                    FROM  rad_ticket rt
                        INNER JOIN rad_ticket_item ri ON (rt.rad_ticket_id = ri.rad_ticket_id AND rt.ticket_id='$ticketId')
                        INNER JOIN service s ON (ri.service_id = s.service_id)
                        LEFT JOIN rad_additional_service_item rsi ON (rsi.rad_ticket_item_id = ri.rad_ticket_item_id)
                        LEFT JOIN pay_insurance_support p ON (s.pay_insurance_support_id = p.pay_insurance_support_id)
                        LEFT JOIN $l10Table l ON (s.name=l.$l10Key)
                    GROUP BY s.service_id";
        $res    =   $this->db->execute($sql);
        while ($res && !$res->EOF) {
            if (!key_exists($res->fields['service_id'], $this->radItemList)) {
                $this->radItemList[$res->fields['service_id']]    =   array(
                   'serviceName'   =>  $res->fields['service_name'],
                   'insuranceType' =>  $res->fields['enum_group_type'],
                   'insuracePrice' =>  $res->fields['insurance_price'],
                   'quantity'      =>  1,
                   'price'         =>  $res->fields['normal_price'] + EnforceType::int($res->fields['additional_price'])
                );
            }
            else {
                $item   =   &$this->radItemList[$res->fields['service_id']];
                $item['quantity']   +=  1;
            }
            $res->moveNext();
        }

        // Get laboratory items
        $sql    =   "SELECT li.service_id, l.$field service_name, li.normal_price,
                        p.price as insurance_price, p.enum_group_type
                    FROM  lab_assignment_ticket lat
                        INNER JOIN lab_ticket lt ON (lat.lab_assignment_ticket_id = lt.lab_assignment_ticket_id AND lat.ticket_id = '$ticketId')
                        INNER JOIN lab_assigned_item li ON (li.lab_ticket_id = lt.lab_ticket_id)
                        INNER JOIN service s ON (li.service_id = s.service_id)
                        LEFT JOIN pay_insurance_support p ON (s.pay_insurance_support_id = p.pay_insurance_support_id)
                        LEFT JOIN $l10Table l ON (s.name=l.$l10Key)";
        $res    =   $this->db->execute($sql);
        while ($res && !$res->EOF) {
            if (!key_exists($res->fields['service_id'], $this->labItemList)) {
                $this->labItemList[$res->fields['service_id']]    =   array(
                   'serviceName'   =>  $res->fields['service_name'],
                   'insuranceType' =>  $res->fields['enum_group_type'],
                   'insuracePrice' =>  $res->fields['insurance_price'],
                   'quantity'      =>  1,
                   'price'         =>  $res->fields['normal_price']
                );
            }
            else {
                $item   =   &$this->labItemList[$res->fields['service_id']];
                $item['quantity']   +=  1;
            }
            $res->moveNext();
        }
    }

    public function updateBillStorage(){
        foreach ($this->radItemList as $key => $item) {
        	$billableItem  =   new ParacliniqueFeeItem($key, $item['serviceName'], L10nHelper::_("Times"), $item['quantity'],
                $item['price'], $item['price'], $item['price'],
                $item['insuranceType'], $item['insuracePrice']);
            $this->storage->addItem(PaymentConstant::ITEM_PARACLINIQUE, $billableItem);
        }
        foreach ($this->labItemList as $key => $item) {
            $billableItem  =   new ParacliniqueFeeItem($key, $item['serviceName'], L10nHelper::_("Times"), $item['quantity'],
                $item['price'], $item['price'], $item['price'],
                $item['insuranceType'], $item['insuracePrice']);
            $this->storage->addItem(PaymentConstant::ITEM_PARACLINIQUE, $billableItem);
        }
    }
}
?>